package com.crm.system.service.impl.messageprompt;

import cn.hutool.core.date.DateTime;
import com.beust.jcommander.internal.Lists;
import com.crm.common.model.DhPageInfo;
import com.crm.common.util.DateUtil;
import com.crm.common.util.DateUtils;
import com.crm.model.entity.messageprompt.MessagePrompt;
import com.crm.model.entity.sys.UserAccount;
import com.crm.model.vo.messageprompt.MessagePromptVO;
import com.crm.service.messageprompt.MessagePromptDbService;
import com.crm.system.service.messageprompt.MessagePromptService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import java.util.Date;
import java.util.List;

/**
 * @Description: 信息提示
 * @Author lixq-f
 * @Date 2021/5/18
 * @Version V1.0
 **/
@Service
public class MessagePromptServiceImpl implements MessagePromptService {

    @Autowired
    private MessagePromptDbService messagePromptDbService;


    /**
     * @Description: 获取消息提示列表
     * @Param: []
     * @Return: java.util.List<com.crm.model.entity.messageprompt.MessagePrompt>
     * @Author: lixq-f
     * @Date: 2021/5/18
     **/
    @Override
    public DhPageInfo<MessagePromptVO> queryMessagePromptList(Integer pageIndex, Integer pageSize) {
        if (pageIndex != null && pageSize != null) {
            PageHelper.startPage(pageIndex, pageSize);
        }
        Date date = new Date();
        DateTime begin = DateUtils.beginOfDay(date);
        DateTime end = DateUtils.endOfDay(date);
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        List<MessagePromptVO> promptList =
                messagePromptDbService.queryMessagePromptList(userAccount.getAccountId(), begin, end);
        if (CollectionUtils.isEmpty(promptList))
            promptList =  Lists.newArrayList();
        PageInfo<MessagePromptVO> pageInfo = new PageInfo<>(promptList);
        return new DhPageInfo<>(pageInfo, promptList);
    }


    /**
     * @Description: 更新消息状态为已读
     * @Param: [messagePrompt]
     * @Return: java.lang.Boolean
     * @Author: lixq-f
     * @Date: 2021/5/18
     **/
    @Override
    public Boolean updateMessageState(String messageId) {
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        MessagePrompt prompt = new MessagePrompt();
        prompt.setMessageId(messageId);
        prompt.setMessageState((short)1);
        prompt.setUpdateTm(DateUtil.getSysDate());
        prompt.setUpdatePeopleId(userAccount.getAccountId());
        return messagePromptDbService.updateMessageState(prompt);
    }


    /**
     * @Description: 未读消息数
     * @Param: []
     * @Return: java.lang.Integer
     * @Author: lixq-f
     * @Date: 2021/5/18
     **/
    @Override
    public Integer unreadMessageCount() {
        UserAccount userAccount = (UserAccount) SecurityUtils.getSubject().getPrincipal();
        Date date = new Date();
        DateTime startTm = DateUtils.beginOfDay(date);
        DateTime endTm = DateUtils.endOfDay(date);
        return messagePromptDbService.unreadMessageCount(userAccount.getAccountId(), startTm, endTm);
    }



}
